//
//  WuLiuDetailViewController.m
//  DPGJ
//
//  Created by ZhuoLaiQiang@Gmail.com on 11-11-23.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "WuLiuDetailViewController.h"
#import "URLFactory.h"
#import "CONSTS.h"
#import "WuLiuDetail.h"
#import "Location.h"

@interface WuLiuDetailViewController() 

- (void)didGetWuLiuDeatil:(TaoBaoServer *)getWuLiuDetailTaoBaoServer_;
- (void)showTableViewWithAnimation;

@end

@implementation WuLiuDetailViewController

@synthesize tid                                     =       _tid;
@synthesize tableView                               =       _tableView;
@synthesize getWuLiuDetailTaoBaoServer              =       _getWuLiuDetailTaoBaoServer;
@synthesize tableViewDataSourceWuLiuDetail          =       _tableViewDataSourceWuLiuDetail;

- (void)dealloc 
{
    [_tableView release];
    LF_RELEASE_SAFELY(_getWuLiuDetailTaoBaoServer);
    LF_RELEASE_SAFELY(_tid);
    LF_RELEASE_SAFELY(_tableViewDataSourceWuLiuDetail);
    
    [super dealloc];
}

- (id)initWithTid:(NSString *)tid_
{
    self = [super init];
    if (self) 
    {
        self.tid = tid_;
    }
    
    return self;
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.navigationItem.title = @"物流详情";
    
    self.navigationController.navigationBar.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    self.tableView.alpha = 0.0f;
    
    NSString *urlStr = [URLFactory get_taobao_logistics_orders_detail_get: _tid];
    self.getWuLiuDetailTaoBaoServer = [[[TaoBaoServer alloc] initWithUrl: urlStr 
                                                                  object: self
                                                      requestIsEndSelect: @selector(didGetWuLiuDeatil:)
                                                     requestIsFailSelect: nil 
                                                            showWaitView: YES 
                                                               urlEncode: NO] autorelease];
    [_getWuLiuDetailTaoBaoServer startAsynchronousRequest];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear: animated];
    
    self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
}

- (void)viewDidUnload
{
    [self setTableView:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

#pragma mark - 自定义函数
- (void)didGetWuLiuDeatil:(TaoBaoServer *)getWuLiuDetailTaoBaoServer_
{
    BOOL successSign = verifyServerRetunSuccess(getWuLiuDetailTaoBaoServer_.dic);
    if (!successSign) 
    {
        /*
         {
         "error_response" =     {
         code = 550;
         msg = "Remote service error";
         "sub_code" = "isv.invalid-parameter:trade_id:P07";
         "sub_msg" = "\U67e5\U8be2\U4e0d\U5230\U7ed3\U679c,\U6216\U8005\U4ea4\U6613id\U4e0d\U5b58\U5728";
         };
         }
         */
        
        NSDictionary *error_response = [getWuLiuDetailTaoBaoServer_.dic objectForKey: @"error_response"];
        NSString *sub_msg = [error_response objectForKey: @"sub_msg"];
        if (!sub_msg || [sub_msg isEqualToString: @""])
        {
            showAlert(ALERT_TITLE, ALERT_MESSAGE);
        }else
        {
            showAlert(ALERT_TITLE, sub_msg);
        }
        showAlert(ALERT_TITLE, @"卖家对本交易创建物流后，方可查询物流信息");
        
        return;
    }
    
    
    
#ifdef DEBUG
    writeJsonToPlist(getWuLiuDetailTaoBaoServer_.dic,  api_taobao_logistics_orders_detail_get);
#endif
    
    /*
     "logistics_orders_detail_get_response" =     {
     shippings =         {
     shipping =             (
     */
    
    //解释获取的数据
    self.tableViewDataSourceWuLiuDetail = [[[WuLiuDetail alloc] init] autorelease];
    NSArray *shipping = [[[getWuLiuDetailTaoBaoServer_.dic objectForKey: @"logistics_orders_detail_get_response"] objectForKey: @"shippings"] objectForKey: @"shipping"];
    NSDictionary *wuliuDetail = [shipping objectAtIndex: 0];
    
    //货物名称 for section0
    _tableViewDataSourceWuLiuDetail.item_title = [wuliuDetail objectForKey: @"item_title"];
    ITTDINFO(@"%@", _tableViewDataSourceWuLiuDetail.item_title);
    
    //物流信息 for sectio1
    _tableViewDataSourceWuLiuDetail.type = getSendKindWithSign([wuliuDetail objectForKey: @"type"]);
    
    _tableViewDataSourceWuLiuDetail.order_code = [NSString stringWithFormat: @"%@", [wuliuDetail objectForKey: @"order_code"]];
    
    _tableViewDataSourceWuLiuDetail.company_name = [wuliuDetail objectForKey: @"company_name"];
    
    _tableViewDataSourceWuLiuDetail.out_sid = [wuliuDetail objectForKey: @"out_sid"];
    
    _tableViewDataSourceWuLiuDetail.status = getWuLiuDingDanZhuangTaiWitSign([wuliuDetail objectForKey: @"status"]);
    
    _tableViewDataSourceWuLiuDetail.freight_payer = getChengDanYunFei([wuliuDetail objectForKey: @"freight_payer"]);
    
    _tableViewDataSourceWuLiuDetail.seller_confirm = [wuliuDetail objectForKey: @"seller_confirm"];
    
    _tableViewDataSourceWuLiuDetail.is_success = [wuliuDetail objectForKey: @"is_success"];
    
    //下单信息 for section
    _tableViewDataSourceWuLiuDetail.created = [wuliuDetail objectForKey: @"created"];
    _tableViewDataSourceWuLiuDetail.modified = [wuliuDetail objectForKey: @"modified"];
    
    //收货信息
    _tableViewDataSourceWuLiuDetail.buyer_nick = [wuliuDetail objectForKey: @"buyer_nick"];
    ITTDINFO(@"%@", [wuliuDetail objectForKey: @"buyer_nick"]);
    NSDictionary *locationDic = [wuliuDetail objectForKey: @"location"];
    Location *location = [[[Location alloc] initWithDataSourceDic: locationDic] autorelease];
    _tableViewDataSourceWuLiuDetail.location = location;
    
    _tableViewDataSourceWuLiuDetail.receiver_name = [wuliuDetail objectForKey: @"receiver_name"];
    _tableViewDataSourceWuLiuDetail.receiver_phone = [wuliuDetail objectForKey: @"receiver_phone"];
    _tableViewDataSourceWuLiuDetail.receiver_mobile = [wuliuDetail objectForKey: @"receiver_mobile"];
    
    
    //预约取货时间
    _tableViewDataSourceWuLiuDetail.delivery_start = [wuliuDetail objectForKey: @"delivery_start"];
    _tableViewDataSourceWuLiuDetail.delivery_end = [wuliuDetail objectForKey: @"delivery_end"];
    
    
    
    
    
    
    
    
    
    [_tableView reloadData];
    
    [self showTableViewWithAnimation];
}

- (void)showTableViewWithAnimation
{
    [UIView beginAnimations: nil context: nil];
    [UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration: 0.5];
    
    _tableView.alpha = 1.0f;
    
    [UIView commitAnimations];
}

#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    NSInteger num = 0;
    if (_tableViewDataSourceWuLiuDetail != nil)
    {
        if (0 == section)
        {
            num = 1;
        }else if (1 == section)
        {
            num = 8;
        }else if (2 == section)
        {
            num = 2;
        }else if (3 == section)
        {
            num = 1;
        }else if (4 == section)
        {
            num = 9;
        }else if (5 == section)
        {
            num = 2;
        }
    }else
    {
        num = 0;
    }
    
    return num;
}

- (UITableViewCell *)findCellWith:(NSString *)identifier_ tableView:(UITableView *)tableView_
{
    UITableViewCell *cell = nil;
    
    cell = [tableView_ dequeueReusableCellWithIdentifier: identifier_];
    if (!cell) 
    {
        NSArray *cellArray = [[NSBundle mainBundle] loadNibNamed: @"WuLiuDeatilCell" owner: nil options: nil];
        
        for (UITableViewCell *tempCell in cellArray)
        {
            if ([tempCell.reuseIdentifier isEqualToString: identifier_])
            {
                cell = tempCell;
                break;
            }
        }
    }
    
    return cell;
    
}

- (void)setLabelText:(NSString *)text_ label:(UILabel *)label_
{
    @try 
    {
        ITTDINFO(@"%@", text_);
        label_.text = text_;
        CGRect frame = label_.frame;
        CGSize size = bubbleView(label_.text, label_.font, frame.size.width);
        label_.frame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, size.height);
    }
    @catch (NSException *exception) 
    {
        
        ITTDINFO(@"%@", exception);
    }
    @finally 
    {
        
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *section_0_row_0 = @"section_0_row_0";
    static NSString *section_1_row_0 = @"section_1_row_0";
    
    NSInteger section = [indexPath section];
    NSInteger row = [indexPath row];
    
    UITableViewCell *cell = nil;
    
    if (0 == section)
    {
        cell = [self findCellWith: section_0_row_0 tableView: tableView];
        
        UILabel *item_title_label = (UILabel *)[cell viewWithTag: 101];
        [self setLabelText: _tableViewDataSourceWuLiuDetail.item_title label: item_title_label];
        
    }else if (1 == section)
    {
        cell = [self findCellWith: section_1_row_0 tableView: tableView];
        UILabel *titleLabel = (UILabel *)[cell viewWithTag: 101];
        
        UILabel *infoLabel = (UILabel *)[cell viewWithTag: 102];
        
        if (0 == row) 
        {
            titleLabel.text = @"物流方式:";
            [self setLabelText: _tableViewDataSourceWuLiuDetail.type label: infoLabel];
            
        }else if (1 == row)
        {
            titleLabel.text = @"物流编号:";
            
            [self setLabelText: _tableViewDataSourceWuLiuDetail.order_code label: infoLabel];
        }else if (2 == row)
        {
            titleLabel.text = @"物流公司:";
            
            [self setLabelText: _tableViewDataSourceWuLiuDetail.company_name label: infoLabel];
        }else if (3 == row)
        {
            titleLabel.text = @"运单编号:";
            [self setLabelText: _tableViewDataSourceWuLiuDetail.out_sid label: infoLabel];
        }
        
        
        else if (4 == row)
        {
            titleLabel.text = @"物流状态:";
            [self setLabelText: _tableViewDataSourceWuLiuDetail.status label: infoLabel];
        }else if (5 == row)
        {
            titleLabel.text = @"承担运费:";
            [self setLabelText: _tableViewDataSourceWuLiuDetail.freight_payer label: infoLabel];
        }else if (6 == row)
        {
            titleLabel.text = @"确认发货:";
            [self setLabelText: _tableViewDataSourceWuLiuDetail.seller_confirm label: infoLabel];
            
        }else if (7 == row)
        {
            titleLabel.text = @"发货成功:";
            [self setLabelText: _tableViewDataSourceWuLiuDetail.is_success label: infoLabel];
        }
    }else if (2 == section)
    {
        cell = [self findCellWith: section_1_row_0 tableView: tableView];
        UILabel *titleLabel = (UILabel *)[cell viewWithTag: 101];
        
        UILabel *infoLabel = (UILabel *)[cell viewWithTag: 102];
        
        if (0 == row)
        {
            titleLabel.text = @"订单创建:";
            [self setLabelText: _tableViewDataSourceWuLiuDetail.created label: infoLabel];
        }else if (1 == row)
        {
            titleLabel.text = @"运单修改:";
            [self setLabelText: _tableViewDataSourceWuLiuDetail.modified label: infoLabel];
        }
    }else if (3 == section)
    {
        cell = [self findCellWith: section_1_row_0 tableView: tableView];
        UILabel *titleLabel = (UILabel *)[cell viewWithTag: 101];
        
        UILabel *infoLabel = (UILabel *)[cell viewWithTag: 102];
        
        if (0 == row)
        {
            titleLabel.text = @"物流跟踪:";
            [self setLabelText: _tableViewDataSourceWuLiuDetail.wuLiuInfo label: infoLabel];
        }
    }else if (4 == section)
    {
        cell = [self findCellWith: section_1_row_0 tableView: tableView];
        UILabel *titleLabel = (UILabel *)[cell viewWithTag: 101];
        
        UILabel *infoLabel = (UILabel *)[cell viewWithTag: 102];
        Location *location = _tableViewDataSourceWuLiuDetail.location;
        
        if (0 == row)
        {
            titleLabel.text = @"买家昵称:";
            [self setLabelText: _tableViewDataSourceWuLiuDetail.buyer_nick label: infoLabel];
        }else if (1 == row)
        {
            titleLabel.text = @"省  份:";
            [self setLabelText: location.state label: infoLabel];
        }else if (2 == row)
        {
            titleLabel.text = @"地  区:";
            [self setLabelText: location.district label: infoLabel];
        }else if (3 == row)
        {
            titleLabel.text = @"城  市:";
            [self setLabelText: location.city label: infoLabel];
        }else if (4 == row)
        {
            titleLabel.text = @"街  道:";
            [self setLabelText: location.address label: infoLabel];
        }else if (5 == row)
        {
            titleLabel.text = @"邮  编:";
            [self setLabelText: location.zip label: infoLabel];
        }else if (6 == row)
        {
            titleLabel.text = @"姓  名:";
            [self setLabelText: _tableViewDataSourceWuLiuDetail.receiver_name label: infoLabel];
        }else if (7 == row)
        {
            titleLabel.text = @"家用电话:";
            [self setLabelText: _tableViewDataSourceWuLiuDetail.receiver_phone label: infoLabel];
        }else if (8 == row)
        {
            titleLabel.text = @"移动电话:";
            [self setLabelText: _tableViewDataSourceWuLiuDetail.receiver_mobile label: infoLabel];
        }
    }else if (5 == section)
    {
        cell = [self findCellWith: section_1_row_0 tableView: tableView];
        UILabel *titleLabel = (UILabel *)[cell viewWithTag: 101];
        
        UILabel *infoLabel = (UILabel *)[cell viewWithTag: 102];
        if (0 == row)
        {
            titleLabel.text = @"开始时间:";
            [self setLabelText: _tableViewDataSourceWuLiuDetail.delivery_start label: infoLabel];
            
        }else
        {
            titleLabel.text = @"结束时间:";
            [self setLabelText: _tableViewDataSourceWuLiuDetail.delivery_end label: infoLabel];
        }
    }
    
    
    else
    {
        cell = [[[UITableViewCell alloc] init] autorelease];
    }
    
    
    
    if (3 == section && 0 == row)
    {
        cell.selectionStyle = UITableViewCellSelectionStyleBlue;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }else
    {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 6;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSString *title = nil;
    if (0 == section)
    {
        title = @"货物名称";
    }else if (1 == section)
    {
        title = @"物流信息";
    }else if (2 == section)
    {
        title = @"下单信息";
    }else if (3 == section)
    {
        title = @"物流跟踪";
    }else if (4 == section)
    {
        title = @"收货信息";
    }else if (5 == section)
    {
        title = @"预约取货时间";
    }
    
    return title;
}

- (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger section = [indexPath section];
    NSInteger row = [indexPath row];
    
    static NSString *section_0_row_0 = @"section_0_row_0";
    static NSString *section_1_row_0 = @"section_1_row_0";
    
    UITableViewCell *cell = nil;// [[[UITableViewCell alloc] init] autorelease];
    
    
    float height = 90;
    if (0 == section)
    {
        NSArray *cellArray = [[NSBundle mainBundle] loadNibNamed: @"WuLiuDeatilCell" owner: nil options: nil];
        
        for (UITableViewCell *tempCell in cellArray)
        {
            if ([tempCell.reuseIdentifier isEqualToString: section_0_row_0])
            {
                cell = tempCell;
                
                UILabel *item_title_label = (UILabel *)[cell viewWithTag: 101];
                
                ITTDINFO(@"%@", _tableViewDataSourceWuLiuDetail.item_title);
                CGSize size = bubbleView( _tableViewDataSourceWuLiuDetail.item_title, item_title_label.font, item_title_label.frame.size.width);
                
                height = size.height + item_title_label.frame.origin.y * 2;
                break;
            }
        }
    }else
    {
        NSArray *cellArray = [[NSBundle mainBundle] loadNibNamed: @"WuLiuDeatilCell" owner: nil options: nil];
        
        for (UITableViewCell *tempCell in cellArray)
        {
            if ([tempCell.reuseIdentifier isEqualToString: section_1_row_0])
            {
                cell = tempCell;
                
                UILabel *infoLabel = (UILabel *)[cell viewWithTag: 102];
                
                ITTDINFO(@"%@", _tableViewDataSourceWuLiuDetail.item_title);
                NSString *infoText = nil;
                if (1 == section)
                {
                    if (0 == row)
                    {
                        infoText = _tableViewDataSourceWuLiuDetail.type;
                    }else if (1 == row)
                    {
                        infoText = _tableViewDataSourceWuLiuDetail.order_code;
                        
                        ITTDINFO(@"%@", infoText);
                    }else if (2 == row)
                    {
                        infoText = _tableViewDataSourceWuLiuDetail.company_name;
                    }else if (3 == row)
                    {
                        infoText = _tableViewDataSourceWuLiuDetail.out_sid;
                    }else if (4 == row)
                    {
                        infoText = _tableViewDataSourceWuLiuDetail.status;
                    }else if (5 == row)
                    {
                        infoText = _tableViewDataSourceWuLiuDetail.freight_payer;
                    }else if (6 == row)
                    {
                        infoText = _tableViewDataSourceWuLiuDetail.seller_confirm;
                    }else if (7 == row)
                    {
                        infoText = _tableViewDataSourceWuLiuDetail.is_success;
                    }
                }else if (2 ==section)
                {
                    if (0 == row)
                    {
                        infoText = _tableViewDataSourceWuLiuDetail.created;
                    }else
                    {
                        infoText = _tableViewDataSourceWuLiuDetail.modified;
                    }
                }else if (3 == section)
                {
                    if (0 == row)
                    {
                        infoText = _tableViewDataSourceWuLiuDetail.wuLiuInfo;
                    }
                }else if (4 == section)
                {
                    Location *location = _tableViewDataSourceWuLiuDetail.location;
                    
                    if (0 == row)
                    {
                        infoText = _tableViewDataSourceWuLiuDetail.buyer_nick;
                    }else if (1 == row)
                    {
                        infoText = location.state;
                    }else if (2 == row)
                    {
                        infoText = location.district;
                    }else if (3 == row)
                    {
                        infoText = location.city;
                    }else if (4 == row)
                    {
                        infoText = location.address;
                    }else if (5 == row)
                    {
                        infoText = location.zip;
                    }else if (6 == row)
                    {
                        infoText = _tableViewDataSourceWuLiuDetail.receiver_name;
                    }else if (7 == row)
                    {
                        infoText = _tableViewDataSourceWuLiuDetail.receiver_phone;
                    }else if (8 == row)
                    {
                        infoText = _tableViewDataSourceWuLiuDetail.receiver_mobile;
                    }
                }
                
                
                
                
                CGSize size = bubbleView( infoText, infoLabel.font, infoLabel.frame.size.width);
                
                height = size.height + infoLabel.frame.origin.y * 2;
                if (height < 39)
                {
                    height = 39;
                }
                
                break;
            }
        }
    }
    
    ITTDINFO(@"  section = %d     row = %d      height = %f", section, row, height);
    
    return height;
}

@end
